package cn.lzx.transaction;

import cn.lzx.domain.User;
import cn.lzx.domain.jpa.UserJpaRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * Created by lizhenxing on 2017/8/4.
 */
@Slf4j
@Service
public class UserService {

    @Autowired
    private UserJpaRepository userJpaRepository;

    @CacheEvict(cacheNames = "user", key = "#{user.identity}")
    public void save(User user) {
        userJpaRepository.save(user);
    }

    @Cacheable(cacheNames = "user", key = "#identity",unless = "#result==null")
    public User getUser(String identity) {
        log.info("缓存未命中, 执行db查询 key: {}", identity);
        return userJpaRepository.queryByIdentity(identity);
    }
}
